package nullby.yoga.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import nullby.yoga.R;
import nullby.yoga.bo.Content;
import nullby.yoga.utils.Extra;
import nullby.yoga.utils.PrefManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ContentAdapter extends ArrayAdapter<Content> {
	private static final int IMAGE_MAX_SIZE = 128;
	private List<Content> objects;
	private LayoutInflater inflater;
	private String programmId;
	private String pref;
//	private Animation left;
//	private Animation right;

	public ContentAdapter(Context context, int textViewResourceId,
			List<Content> objects, String programmId) {
		super(context, textViewResourceId, objects);
		this.objects = objects;
		this.programmId = "1";
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		pref = null;

		if ("".equals(PrefManager.get().getSex(this.programmId))) {
			if (Extra.BOY.equals(PrefManager.get().getSex(null))) {
				pref = "m_";
			} else if (Extra.GIRL.equals(PrefManager.get().getSex(null))) {
				pref = "w_";
			}
		} else {
			if (Extra.BOY.equals(PrefManager.get().getSex(this.programmId))) {
				pref = "m_";
			} else if (Extra.GIRL.equals(PrefManager.get().getSex(
					this.programmId))) {
				pref = "w_";
			}
		}
//		left = AnimationUtils.loadAnimation(getContext(), R.anim.left);
//		right = AnimationUtils.loadAnimation(getContext(), R.anim.right);
	}

	@Override
	public View getView(int position, View vi, ViewGroup parent) {
		if (vi == null) {
			vi = inflater.inflate(R.layout.content_item, null);
		}
		Content content = objects.get(position);
		ImageView iv = (ImageView) vi.findViewById(R.id.ivContentIconItem);

		// Drawable dr = getContext().getResources().getDrawable(
		// getContext().getResources().getIdentifier(
		// pref.concat(content.getIcon()), "drawable",
		// getContext().getPackageName()));

		// iv.setImageResource(getContext().getResources().getIdentifier(
		// pref.concat(content.getIcon()), "drawable",
		// getContext().getPackageName()));
		iv.setImageBitmap(decodeFile(getContext().getResources().getIdentifier(
				pref.concat(content.getIcon()), "drawable",
				getContext().getPackageName())));
		iv.setPadding(0, 0, 20, 0);
		TextView t = (TextView) vi.findViewById(R.id.tvContentNameItem);
		t.setText(("".equals(content.getName())) ? content.getDescription()
				: content.getName());

		vi.setPadding(20, 10, 10, 10);
//
//		if (position % 2 == 0) {
//			vi.startAnimation(left);
//		} else {
//			vi.startAnimation(right);
//		}
		// t.startAnimation(hyperspaceJump);

		return vi;
	}

	private Bitmap decodeFile(final Integer idResource) {
		Bitmap b = null;
		try {
			// Decode image size
			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;

			InputStream fis = getContext().getResources().openRawResource(
					idResource);
			BitmapFactory.decodeStream(fis, null, o);
			fis.close();

			int scale = 1;
			if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
				scale = (int) Math.pow(
						2,
						(int) Math.round(Math.log(IMAGE_MAX_SIZE
								/ (double) Math.max(o.outHeight, o.outWidth))
								/ Math.log(0.5)));
			}

			// Decode with inSampleSize
			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = scale;
			fis = getContext().getResources().openRawResource(idResource);
			b = BitmapFactory.decodeStream(fis, null, o2);
			fis.close();
		} catch (IOException e) {
		}
		return b;
	}
}
